home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
cpp_libs
/
varia
/
interp18.lha
/
interp-1.8
/
Ast.cc
< prev
next >
Wrap
C/C++ Source or Header
|
1990-01-20
|
5KB
|
249 lines
// Ast.cc -- the definition of the member functions for class Ast.
// Copyright (C) 1989 Carey Richard Murphey.
// (rich@rice.edu) 5310 Rutherglenn, Houston, TX 77096
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 1, or (at your option)
// any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#include "Ast.h"
#if !defined(__OPTIMIZE__) || defined(INLINE)
#ifndef INLINE
#define INLINE
#endif
// class Node
INLINE friend ostream& operator << (ostream& Stream, Node* p)
{
return p->echo (Stream);
}
INLINE Node:: operator double ()
{
return this->eval();
}
// class NoOp
INLINE double NoOp:: eval()
{
return 0.;
}
INLINE ostream& NoOp:: echo (ostream& Stream) return r(Stream);
{
return;
}
// class Sym
INLINE double Sym:: eval()
{
return (double) *(*table)[name];
}
INLINE Sym:: operator double ()
{
return (double) *(*table)[name];
}
INLINE Sym:: operator int ()
{
return (int) *(*table)[name];
}
INLINE double Sym:: func (double p)
{
return (*table)[name]->func (p);
}
INLINE void Sym:: assign (double p)
{
(*table)[name]->assign (p);
}
INLINE ostream& Sym:: echo (ostream& Stream)
{
return Stream << name;
}
// class Func
INLINE double Func:: eval()
{
return left->func (right->eval ());
}
INLINE ostream& Func:: echo (ostream& Stream)
{
return Stream << left << " (" << right << ")";
}
// class Assign
INLINE double Assign:: eval ()
{
left->assign (right->eval ());
return (double) *left;
}
INLINE ostream& Assign:: echo (ostream& Stream)
{
return Stream << left << " = " << right;
}
// class PostIncrement
INLINE double PostIncrement:: eval ()
{
double t = (double) *left;
left->assign ( 1. + (double) *left);
return t;
}
INLINE ostream& PostIncrement:: echo (ostream& Stream)
{
return Stream << left << "++";
}
// class PostDecrement
INLINE double PostDecrement:: eval ()
{
double t = (double) *left;
left->assign ( -1. + (double) *left);
return t;
}
INLINE ostream& PostDecrement:: echo (ostream& Stream)
{
return Stream << left << "--";
}
// class PreIncrement
INLINE double PreIncrement:: eval ()
{
left->assign ( 1. + (double) *left);
return (double) *left;
}
INLINE ostream& PreIncrement:: echo (ostream& Stream)
{
return Stream << "++" << left;
}
// class PreDecrement
INLINE double PreDecrement:: eval ()
{
left->assign ( -1. + (double) *left);
return (double) *left;
}
INLINE ostream& PreDecrement:: echo (ostream& Stream)
{
return Stream << "--" << left;
}
// class If
INLINE double If:: eval ()
{
if(0.==left->eval()) return 0.;
return right->eval();
}
INLINE ostream& If:: echo (ostream& Stream)
{
return Stream << "if (" << left << ") " << right;
}
// class While
INLINE double While:: eval ()
{
while (0.!=left->eval ()) right->eval();
return 0.;
}
INLINE ostream& While:: echo (ostream& Stream)
{
return Stream << "while (" << left << ") " << right;
}
// class Statements
INLINE double Statements:: eval ()
{
left->eval ();
return right->eval ();
}
INLINE ostream& Statements:: echo (ostream& Stream)
{
return Stream << "{" << left << "; " << right << ";}";
}
// class Plus
INLINE double Plus:: eval ()
{
return left->eval () + right->eval ();
}
INLINE ostream& Plus:: echo (ostream& Stream)
{
return Stream << left << " + " << right;
}
// class Minus
INLINE double Minus:: eval ()
{
return left->eval () - right->eval ();
}
INLINE ostream& Minus:: echo (ostream& Stream)
{
return Stream << left << " - " << right;
}
// class Times
INLINE double Times:: eval ()
{
return left->eval () * right->eval ();
}
INLINE ostream& Times:: echo (ostream& Stream)
{
return Stream << left << " * " << right;
}
// class Divide
INLINE double Divide:: eval ()
{
double t = right->eval ();
if (t == 0.)
{
cerr << "Error: divide by zero.";
return 0.;
}
else return left->eval () / t;
}
INLINE ostream& Divide:: echo (ostream& Stream)
{
return Stream << left << " / " << right;
}
// class Pow
INLINE double Pow:: eval ()
{
return pow (left->eval (), right->eval ());
}
INLINE ostream& Pow:: echo (ostream& Stream)
{
return Stream << left << "^" << right;
}
// class Double
INLINE double Double:: eval ()
{
return value;
}
INLINE ostream& Double:: echo (ostream& Stream)
{
return Stream << value;
}
// class Uminus
INLINE double Uminus:: eval ()
{
return - left->eval ();
}
INLINE ostream& Uminus:: echo (ostream& Stream)
{
return Stream << "- " << left;
}
#endif